package cn.gjculture.shopping.api.export;

import cn.gjculture.shopping.api.entity.Customer;
import cn.gjculture.shopping.api.entity.GoodsRecord;
import cn.gjculture.shopping.api.entity.Order;
import cn.gjculture.shopping.api.service.GoodsService;
import cn.gjculture.shopping.api.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;

@Slf4j
@Component
public class OrderExportExcel extends ExportExcel<Order> {

    @Autowired
    GoodsService goodsService;

    @Override
    public void addTitle() {
        String[] titles = {"订单编号", "合计支付", "合计使用积分", "购买账号", "购买人昵称","会员级别", "状态", "退换货状态", "创建时间", "支付时间",
                "商品编码", "商品名称", "商品支付金额", "商品使用积分", "售卖价", "成本","普通会员折扣", "定制会员折扣", "型号",
                "颜色", "数量", "收件人", "电话", "地址", "供应商名称"};
        this.setTitles(Arrays.asList(titles));
    }

    @Override
    public void addData() {
        if (!CollectionUtils.isEmpty(getDatas())) {
            int y = getPosition()[1];
            for (Order o : getDatas()) {
                if (!CollectionUtils.isEmpty(o.getGoodsRecords())) {
                    for (GoodsRecord gr : o.getGoodsRecords()) {
                        if(o.getOrderType()== Order.OrderType.OIL_CARD_CHARGE.getValue()||o.getOrderType()== Order.OrderType.OIL_CARD_BUY.getValue()){
                            collectOilRecord(gr);
                        }else{
                            if(o.getCustomer()==null) continue;
                            collectGoodsRecord(o.getCustomer().getLevel(), gr);
                        }
                        Row row = this.sheet.createRow(y++);
                        row.createCell(0).setCellValue(o.getOrderNo().toString());
                        row.createCell(1).setCellValue(o.getOrderTotalSum());
                        row.createCell(2).setCellValue(o.getUseScore());
                        row.createCell(3).setCellValue(o.getCustomer().getCustomerNo());
                        row.createCell(4).setCellValue(o.getCustomer().getNickname());
                        row.createCell(5).setCellValue(o.getCustomer().getLevel());
                        row.createCell(6).setCellValue(formatterStatus(o.getStatus()));
                        row.createCell(7).setCellValue(formatterRefundStatus(o.getRefundStatus()));
                        row.createCell(8).setCellValue(o.getInsertTimeStr());
                        if(o.getPayTime()!=null){
                            row.createCell(9).setCellValue(DateUtil.getDate(o.getPayTime()));
                        }else{
                            row.createCell(9).setCellValue("");
                        }
                        row.createCell(10).setCellValue(gr.getGoodsNo());
                        row.createCell(11).setCellValue(gr.getGoodsName());
                        row.createCell(12).setCellValue(gr.getGoodsPaySum());
                        row.createCell(13).setCellValue(gr.getGoodsUseScore());
                        row.createCell(14).setCellValue(gr.getDiscountPrice());
                        row.createCell(15).setCellValue(gr.getPrimeCost());
                        row.createCell(16).setCellValue(gr.getDiscount() + "%");
                        if(gr.getSuperDiscount()!=null){
                            row.createCell(17).setCellValue(gr.getSuperDiscount() + "%");
                        }else{
                            row.createCell(17).setCellValue("");
                        }
                        row.createCell(18).setCellValue(gr.getGoodsStand());
                        row.createCell(19).setCellValue(gr.getGoodsColor());
                        row.createCell(20).setCellValue(gr.getCount().toString());
                        if(o.getAddress()!=null){
                            row.createCell(21).setCellValue(o.getAddress().getReceiver());
                            row.createCell(22).setCellValue(o.getAddress().getPhone());
                            row.createCell(23).setCellValue(o.getAddress().getProvinceId()+o.getAddress().getCityId()
                                    +o.getAddress().getAreaId()+o.getAddress().getDetailAddress());
                        }else{
                            row.createCell(21).setCellValue("");
                            row.createCell(22).setCellValue("");
                            row.createCell(23).setCellValue("");
                        }
                        if(gr.getSupplier()!=null){
                            row.createCell(24).setCellValue(gr.getSupplier().getSupplierName());
                        }
                    }
                }
            }
            this.setPosition(new int[]{0, 0});
        }

    }

    private void collectOilRecord(GoodsRecord gr) {
        BigDecimal discount = null;
        if(gr.getDiscount()!=null){
            discount = new BigDecimal(gr.getDiscount().trim()).divide(new BigDecimal(100));
        }
        if(discount!=null){
           setUserScore(discount,gr);
        }
    }

    private void collectGoodsRecord(Integer level, GoodsRecord gr) {
        BigDecimal discount = null;
        if (level >= Customer.Level.VIP1_1.getValue()) {
            if(!StringUtils.isEmpty(gr.getSuperDiscount())){
                discount = new BigDecimal(gr.getSuperDiscount().trim()).divide(new BigDecimal(100));
            }

        } else {
            if(!StringUtils.isEmpty(gr.getDiscount())){
                discount = new BigDecimal(gr.getDiscount().trim()).divide(new BigDecimal(100));
            }
        }
        if(discount!=null){
            setUserScore(discount,gr);
        }
        gr.setSupplier(goodsService.querySupplierByGoodsId(gr.getGoodsId()));

    }

    private void setUserScore(BigDecimal discount,GoodsRecord gr) {
        BigDecimal useScore = new BigDecimal(gr.getDiscountPrice().trim()).multiply(discount);
        gr.setGoodsUseScore(useScore.setScale(2, BigDecimal.ROUND_DOWN).toString());
        gr.setGoodsPaySum(new BigDecimal(gr.getDiscountPrice().trim()).subtract(useScore)
                .setScale(2, BigDecimal.ROUND_DOWN).toString());
    }

    private String formatterRefundStatus(Integer refundStatus) {
        if(refundStatus==null){
            return "";
        }
        if (2 == refundStatus) {
            return "退货发起";
        }
        if (3 == refundStatus) {
            return "拒绝退款";
        }
        if (4 == refundStatus) {
            return "同意退款";
        }
        if (5 == refundStatus) {
            return "添加退货物流信息";
        }
        if (6 == refundStatus) {
            return "收到货物并退款";
        }
        if (8 == refundStatus) {
            return "退款成功";
        }
        return "";
    }

    private String formatterStatus(Integer o) {
        if (1 == o) {
            return "新建";
        }
        if (8 == o) {
            return "已付款";
        }
        if (9 == o) {
            return "已发货";
        }
        if (10 == o) {
            return "完成";
        }
        if (99 == o) {
            return "取消或者删除";
        }
        return "";
    }


}
